//
//  TZShowImageView.m
//  BSBDJ
//
//  Created by user on 16/5/26.
//  Copyright © 2016年 Burning. All rights reserved.
//

#import "TZShowImageView.h"
#import "TZEssenceItem.h"
#import "TZProgressView.h"
#import "TZBigPictureVc.h"
@interface TZShowImageView()<UIGestureRecognizerDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *zhanwei_View;
@property (weak, nonatomic) IBOutlet UIImageView *picture_ImageView;
@property (weak, nonatomic) IBOutlet UIButton *plus_Btn;
@property (weak, nonatomic) IBOutlet TZProgressView *progress_View;
@property (weak, nonatomic) IBOutlet UIImageView *gif_ImageView;

@end


@implementation TZShowImageView

- (void)awakeFromNib{
    UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(plusPictureBtnClick:)];
    tap.delegate = self;
    [self.picture_ImageView addGestureRecognizer:tap];
}

+ (instancetype)showImageView{
    return [[NSBundle mainBundle]loadNibNamed:@"TZShowImageView" owner:nil options:nil].lastObject;
}

- (IBAction)plusPictureBtnClick:(UIButton *)sender {
    NSLog(@"点");
    TZBigPictureVc *bigPicVc = [TZBigPictureVc new];
    bigPicVc.item = self.item;
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:bigPicVc animated:YES completion:nil];
}

- (void)setItem:(TZEssenceItem *)item{
    _item = item;
    
   [self.picture_ImageView sd_setImageWithURL:[NSURL URLWithString:item.small_image] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
       
       self.progress_View.hidden = NO;
       self.zhanwei_View.hidden = NO;
       CGFloat progress = 1.0 *receivedSize / expectedSize;
       [self.progress_View setProgress:progress animated:NO];

   } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
       self.progress_View.hidden = YES;
       self.zhanwei_View.hidden = YES;
       
//       UIGraphicsBeginImageContext(self.picture_ImageView.size);
//       CGFloat width = self.picture_ImageView.width;
//       CGFloat height = width *item.height / item.width;
//       [image drawInRect:CGRectMake(0, 0, width, height)];
//       
//       self.picture_ImageView.image = UIGraphicsGetImageFromCurrentImageContext();
       self.picture_ImageView.image = image;
       UIGraphicsEndImageContext();
       
       
       
   }];
    //  是否是GIF
    self.gif_ImageView.hidden = !item.is_gif;
    
    //如果图片太大 压缩并且显示 更多按钮
    if (item.is_BigImage) {
        self.plus_Btn.hidden = NO;
        self.picture_ImageView.contentMode = UIViewContentModeScaleAspectFit;
    }else {
        self.plus_Btn.hidden = YES;
        self.picture_ImageView.contentMode = UIViewContentModeScaleToFill;
    }
}



@end
